//
//  RowTextFiledCell.m
//  
//  jhf
//
//  Created by Mac on 2019/8/9.
//  Copyright © 2019 Mac. All rights reserved.
//

#import "RowTextFiledCell.h"
#import "RowInputBaseM.h"

@interface RowTextFiledCell ()

@end

@implementation RowTextFiledCell


- (void)setup {
    [self.bgV addSubview:self.titleLB];
    [self.bgV addSubview:self.subTitleLB];
    
    [self.bgV addSubview:self.needLB];
    [self.bgV addSubview:self.unitLB];
    [self.bgV addSubview:self.rightIMG];
    [self.bgV addSubview:self.leftIMG];
    [self.bgV addSubview:self.valueTF];
    
    [self.titleLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.bgV);
        make.left.equalTo(self.bgV).offset(15);
        make.top.equalTo(self.bgV).offset(15);
    }];
    
    [self.subTitleLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.bgV);
        make.left.equalTo(self.titleLB.mas_right).offset(4);
    }];
    self.subTitleLB.backgroundColor = [UIColor randomColor];
    
    [self.leftIMG mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.bgV);
        make.left.equalTo(self.bgV).offset(15);
    }];
    
    [self.needLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.titleLB);
        make.left.equalTo(self.titleLB.mas_right).offset(2);
    }];
    
    [self.unitLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.titleLB);
        make.right.equalTo(self.bgV).offset(-15);
    }];
    
    [self.rightIMG mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.titleLB);
        make.right.equalTo(self.bgV).offset(-15);
    }];
    
    
    [self.valueTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.titleLB);
        make.left.equalTo(self.subTitleLB.mas_right).offset(8);
        make.right.equalTo(self.unitLB.mas_left).offset(-4);
        make.width.mas_greaterThanOrEqualTo(120);
        make.height.mas_greaterThanOrEqualTo(32);
    }];
    
    // 限制输入
    @weakify(self)
    [[self.valueTF.rac_textSignal filter:^BOOL(NSString * _Nullable value) {
        @strongify(self)
        return self.model && self.valueTF.markedTextRange == nil;
    }] subscribeNext:^(NSString * _Nullable x) {
        @strongify(self)
        BindModel *bind = (BindModel *)self.model;
        RowInputBaseM *baseModel = (RowInputBaseM *)bind.model;
        NSString *str = [InputLimitHelper limitInput:self.valueTF conifg:baseModel];
        if (baseModel.dataKey) {
//            NSMutableDictionary *tempParam = [NSMutableDictionary dictionary];
//            [tempParam setValue:str forKey:baseModel.dataKey];
//            bind.data = tempParam;
            [bind.data setValue:str forKey:baseModel.dataKey];
        }
        if (baseModel.monitMethod && baseModel.monitMethod.length > 0) {
            [bind.action run:baseModel.monitMethod any:bind];
        }
        baseModel.value = str;
        baseModel.inputControl.textStr = str;
    }];
}

- (void)bindTo:(__kindof id<BindProtocol>)model; {
    [super bindTo:model];
    BindModel *bind = (BindModel *)model;
    RowInputBaseM *config = (RowInputBaseM *)bind.model;
    
    if (config.titleLBControl) { /// 标题
        [config.titleLBControl controlLB:self.titleLB];
        
        // 顶部
        CGFloat topCons = config.titleLBControl.vTopCons ?: 15;
        // 左侧
        CGFloat leftCons = config.titleLBControl.vLeftCons ?: 15;
        [self.titleLB mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.bgV).offset(topCons);
            make.left.equalTo(self.bgV).offset(leftCons);
        }];
        if (config.titleLBControl.vWidthCons) {
            [self.titleLB mas_updateConstraints:^(MASConstraintMaker *make) {
                make.width.mas_offset(config.titleLBControl.vWidthCons);
            }];
        }
    }else {
        self.titleLB.text = @"";
    }
    
    if (config.subTitleLBControl) { /// 副标题
        [config.subTitleLBControl controlLB:self.subTitleLB];

        // 顶部
        CGFloat topCons = config.titleLBControl.vTopCons ?: 0;
        // 左侧
//        CGFloat leftCons = config.titleLBControl.vLeftCons ?: 0;
        [self.subTitleLB mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.bgV).offset(topCons);
//            make.left.equalTo(self.titleLB.mas_right).offset(leftCons);
        }];
        if (config.subTitleLBControl.vWidthCons) {
            [self.subTitleLB mas_updateConstraints:^(MASConstraintMaker *make) {
                make.width.mas_offset(config.subTitleLBControl.vWidthCons);
            }];
        }
    }else {
        self.subTitleLB.text = @"";
    }
    
    if (config.unitLBControl) { /// 单位
        [config.unitLBControl controlLB:self.unitLB];
        CGFloat rightCons = config.unitLBControl.vRightCons ? -config.unitLBControl.vRightCons : -15;
        [self.unitLB mas_updateConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.bgV).offset(rightCons);
            if (config.unitLBControl.vWidthCons > 0) {
                make.width.mas_equalTo(config.unitLBControl.vWidthCons);
            }else {
                make.width.mas_equalTo(config.unitLBControl.labelWidth);
            }
        }];
    }else {
        self.unitLB.text = @"";
        [self.unitLB mas_updateConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.bgV).offset(-15);
            make.width.mas_equalTo(0);
        }];
    }
    
    if (config.inputControl) {  /// 输入框
        [config.inputControl controlTextField:self.valueTF];
        
        // 修复本身有值的数据
//        if (!bind.data && config.dataKey && config.inputControl.textStr && config.inputControl.textStr.clearSpace > 0) {
        if (config.dataKey && config.inputControl.textStr && config.inputControl.textStr.clearSpace > 0) {
//            NSMutableDictionary *tempParam = [NSMutableDictionary dictionary];
//            [tempParam setValue:config.inputControl.textStr forKey:config.dataKey];
//            bind.data = tempParam;
            [bind.data setValue:config.inputControl.textStr forKey:config.dataKey];
        }
        
        CGFloat minWidthCons = config.inputControl.vMinWidthCons ?: 120;
        CGFloat leftCons = config.inputControl.vLeftCons ?: 4;
        CGFloat rightCons = config.inputControl.vRightCons ?: -4;
        [self.valueTF mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.mas_greaterThanOrEqualTo(minWidthCons);
            make.left.equalTo(self.subTitleLB.mas_right).offset(leftCons);
            make.right.equalTo(self.unitLB.mas_left).offset(rightCons);
        }];
    }else {
        self.valueTF.text = @"";
    }
    
    if (config.needLBControl) {  /// * 必传
        [config.needLBControl controlLB:self.needLB];
        if (config.needLBControl.vLocation > 0) {
            if (config.needLBControl.vLocation == 1) { /// 左侧
                CGFloat leftCons = config.needLBControl.vRightCons ?: 2;
                [self.needLB mas_remakeConstraints:^(MASConstraintMaker *make) {
                    make.centerY.equalTo(self.titleLB);
                    make.right.equalTo(self.titleLB.mas_left).offset(-leftCons);
                }];
            }else if (config.needLBControl.vLocation == 3) { /// 右侧
                CGFloat rightCons = config.needLBControl.vLeftCons ?: 2;
                [self.needLB mas_remakeConstraints:^(MASConstraintMaker *make) {
                    make.centerY.equalTo(self.titleLB);
                    make.left.equalTo(self.titleLB.mas_right).offset(rightCons);
                }];
            }
        }
    }
    self.needLB.hidden = !config.needLBControl;
    
    self.rightIMG.hidden = true;
    self.leftIMG.hidden = true;
    
    /// 右侧图片约束
    if (config.rightImgControl) {
        self.rightIMG.hidden = false;
        [config.rightImgControl controlIMG:self.rightIMG blockBool:nil];
        CGFloat rightCons = config.rightImgControl.vRightCons ? -config.rightImgControl.vRightCons : -15;
        [self.rightIMG mas_updateConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.bgV).offset(rightCons);
        }];
        if (config.rightImgControl.vWidthCons && config.rightImgControl.vHeightCons) {
            CGFloat heightCons = config.rightImgControl.vHeightCons;
            CGFloat widthCons = config.rightImgControl.vWidthCons;
            [self.rightIMG mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo(heightCons);
                make.width.mas_equalTo(widthCons);
            }];
        }
    }
    
    /// 左侧图片约束
    if (config.leftImgControl) {
        self.leftIMG.hidden = false;
        [config.leftImgControl controlIMG:self.leftIMG blockBool:nil];
        // 左侧
        CGFloat leftCons = config.leftImgControl.vLeftCons ?: 15;
        [self.leftIMG mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.bgV).offset(leftCons);
        }];
        if (config.leftImgControl.vWidthCons && config.leftImgControl.vHeightCons) {
            CGFloat heightCons = config.leftImgControl.vHeightCons;
            CGFloat widthCons = config.leftImgControl.vWidthCons;
            [self.leftIMG mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo(heightCons);
                make.width.mas_equalTo(widthCons);
            }];
        }
    }
    
    //
    //    /// 标题设置 没有 输入框左移动
    //    if (config.titleLBControl.textLeftCons.clearSpace.length == 0) {
    //        self.titleLBWidthCons.constant = 0;
    //    }else {
    //        self.titleLBWidthCons.constant = 72;
    //    }
    
    // 初始化默认参数 用于修改什么的
//    if (config.dataKey && config.value) {
//        NSMutableDictionary *tempParam = [NSMutableDictionary dictionary];
//        [tempParam setValue:config.value forKey:config.dataKey];
//        bind.data = tempParam;
//        [bind.data setValue:config.value forKey:config.dataKey];
//    }
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

//MARK: -lazy
- (UILabel *)unitLB {
    if (!_unitLB) {
        _unitLB = [UILabel new];
    }
    return _unitLB;
}

- (UITextField *)valueTF {
    if (!_valueTF) {
        _valueTF = [[UITextField alloc] init];
    }
    return _valueTF;
}

- (UILabel *)needLB {
    if (!_needLB) {
        _needLB = [UILabel new];
        _needLB.backgroundColor = [UIColor redColor];
        _needLB.text = @"*";
    }
    return _needLB;
}

- (UILabel *)titleLB {
    if (!_titleLB) {
        _titleLB = [UILabel new];
    }
    return _titleLB;
}

- (UILabel *)subTitleLB {
    if (!_subTitleLB) {
        _subTitleLB = [UILabel new];
    }
    return _subTitleLB;
}

- (UIImageView *)rightIMG {
    if (!_rightIMG) {
        _rightIMG = [[UIImageView alloc] init];
    }
    return _rightIMG;
}

- (UIImageView *)leftIMG {
    if (!_leftIMG) {
        _leftIMG = [[UIImageView alloc] init];
    }
    return _leftIMG;
}

@end
